<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\URL;
class BaseController extends Controller
{
    //视图数据
    protected $viewData = [];

    /**
     * 构造函数
     */
    public function __construct()
    {

    }
    /**
     * 信息提示页.
     *
     * @param string $type success|error|info|warning|404|500
     * @param string $msg
     * @param string $redirectUrl
     * @param array  $otherData
     */
    public function showMsg($type = 'success', $msg = '', $url = '', $otherData = [])
    {
        $data = [];
        if ($url){
            if (URL::isValidUrl($url)){
                $data['url'] = $url;
            }else{
                $data['url'] = route($url);
            }
        }else{
            $data['url'] = URL::previous();
        }
        $data['message']     = $msg;
        $data['jumpTime'] = 1000;
        $templateFile = 'admin.shared.showmsg';
        $httpStatus = 200;
        $bgClass = 'bg-primary';
        switch ($type) {
            case 'success':
                $data['title'] = '操作成功';
                $bgClass = 'bg-success';
                break;
            case 'error':
                $data['title'] = '操作失败';
                $bgClass = 'bg-danger';
                break;
            case 'info':
                $data['title'] = '消息提示';
                $bgClass = 'bg-info';
                break;
            case 'warning':
                $data['title'] = '错误警告';
                $bgClass = 'bg-warning';
                break;
            default:
                $data['title']  = '未知信息';
        }
        $data['bgClass'] = $bgClass;
        $data['status'] = $type;
        $data['data']  = $otherData;
        $requestType = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] ? strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) : '';
        if ($requestType == 'xmlhttprequest') {
            return response()
                ->json($data);
        } else {
            return response()
                ->view($templateFile, $data, $httpStatus);
        }
    }
}
